package com.dw.widget.slideshow.thread;

import java.io.File;
import java.io.FilenameFilter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.dw.widget.slideshow.db.helper.ImageDBHelper;
import com.dw.widget.slideshow.db.helper.ScanFolderHelper;
import com.dw.widget.slideshow.db.object.ImageData;
import com.dw.widget.slideshow.db.object.ScanFolderData;
import com.dw.widget.slideshow.objects.Size;
import com.dw.widget.slideshow.utils.AppCommon;
import com.dw.widget.slideshow.utils.ApplicationEx;
import com.dw.widget.slideshow.utils.ImageUtils;
import com.dw.widget.slideshow.utils.db.DataObject;
import com.dw.widget.slideshow.utils.log.Log;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;

public class ScanFolderImageThread extends Thread {

	private Context mContext;
	private Handler mHandler;
	private final ApplicationEx _APP = ApplicationEx.INSTANCE();
	private final String TAG = "ScanFolderImageThread";
	
	public ScanFolderImageThread(Context context, Handler handler)
	{
		mContext = context;
		mHandler = handler;
		
		Process.setThreadPriority(MAX_PRIORITY);
	}
	
	@Override
	public void run() {

		Message msg = mHandler.obtainMessage();
		Bundle data = msg.getData();
		
		ScanFolderHelper dbHelper = new ScanFolderHelper(mContext, _APP.DB_NAME, ScanFolderData.class);
		ScanFolderData condition = new ScanFolderData();
		condition.setScaned(_APP.NO);
		List<DataObject> folders = dbHelper.query(condition);
		
		Log.d(mContext, TAG, String.format("about to scan %1$d folders ", folders.size()));
		
		int scanedCount = 0;
		ImageDBHelper imageHelper = new ImageDBHelper(mContext, _APP.DB_NAME, ImageData.class);
		for(DataObject obj : folders)
		{
			
			ScanFolderData folderData = (ScanFolderData)obj;
			String imageDir = folderData.getFolder();
			if(imageDir == null || "".equals(imageDir))
			{
				continue;
			}
			
			File[] imagePath = ImageUtils.listImageFiles(imageDir);
			
			if(imagePath == null || imagePath.length <= 0)
			{
				
				// 置为已扫描
				folderData.setScaned(_APP.YES);
				dbHelper.update(folderData);
				continue;
			}
			
			synchronized (_APP.DB_MODIFY_LOCK) {
				
				for(File path : imagePath)
				{
					AppCommon.insertImage2Db(imageHelper, mContext, path);
					scanedCount ++;
					
					if(scanedCount == _APP.INIT_ADD_NUMBER)    // 如果达到初始添加数后则开始切换
					{
						mHandler.sendEmptyMessage(_APP.THREAD_MSG_ADDINIT);
					}
				}
			}
			
			// 置为已扫描
			folderData.setScaned(_APP.YES);
			dbHelper.update(folderData);
		}
		
		Log.d(mContext, TAG, String.format("Scaned files %1$d", scanedCount));
		if(mHandler != null)
		{
			msg.what = _APP.THREAD_MSG_SCANFOLDER;
			data.putInt(_APP.THREAD_PARA_SCANEDNUM, scanedCount);
			
			if(scanedCount >= _APP.INIT_ADD_NUMBER)
			{
				data.putString(_APP.THREAD_PARA_INITADDED, _APP.YES);
			}
			else
			{
				data.putString(_APP.THREAD_PARA_INITADDED, _APP.NO);
			}
			
			mHandler.sendMessage(msg);
		}
	}

	
}
